package com.example.tys.baseapplication.View;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.SweepGradient;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * 雷达扫描图
 *
 * @author tys
 * @date 2019/4/9
 */

public class RadioView extends View {

    private SweepGradient sweepGradient;
    private Paint mScanPaint;
    private int mHeight;
    private int mWidth;
    private Paint mBgCirclePaint;

    /**
     * 整体圆半径
     */
    private int mRadios;

    /**
     * 圆个数
     */
    private int mTotalCircle;

    /**
     * 圆之间的间隙
     */
    private int mSpaceVal;

    /**
     * 当前选择哪个圆(高亮)[0, mTotalCircle]
     */
    private int mSelected = -1;

    /**
     * 旋转角度(扫描)
     */
    private float mRateDegrees;

    private ValueAnimator mAnimator;

    public RadioView(Context context) {
        this(context, null);
    }

    public RadioView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mScanPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mScanPaint.setStyle(Paint.Style.FILL);
        mBgCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBgCirclePaint.setColor(Color.WHITE);
        mBgCirclePaint.setStyle(Paint.Style.STROKE);
        mSpaceVal = dp2px(25);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int wMode = MeasureSpec.getMode(widthMeasureSpec);
        int hMode = MeasureSpec.getMode(heightMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawBackgroundCircle(canvas);
        drawScanner(canvas);
    }


    /**
     * 画扫描，{@link #mRateDegrees}旋转角度
     *
     * @param canvas
     */
    private void drawScanner(Canvas canvas) {
        canvas.save();
        canvas.rotate(mRateDegrees, mWidth / 2, mHeight / 2);
        canvas.drawCircle(mWidth / 2, mHeight / 2, Math.min(mWidth / 2, mHeight / 2), mScanPaint);
        canvas.restore();
    }

    private void drawBackgroundCircle(Canvas canvas) {
        drawBackgroundColor(canvas);
        drawBackground(canvas);
    }

    /**
     * 画一圈一圈的圆，根据{@link #mSelected}高亮
     *
     * @param canvas
     */
    private void drawBackground(Canvas canvas) {
        for (int j = 0, minRadios = mRadios; j <= mTotalCircle; j++) {
            if (j == mSelected) {
                mBgCirclePaint.setTypeface(Typeface.DEFAULT_BOLD);
                mBgCirclePaint.setColor(Color.WHITE);
                mBgCirclePaint.setStrokeWidth(2);
            } else {
                mBgCirclePaint.setTypeface(Typeface.DEFAULT);
                mBgCirclePaint.setColor(0xFFDDDDDD);
                mBgCirclePaint.setStrokeWidth(1);
            }
            mBgCirclePaint.setStyle(Paint.Style.STROKE);
            canvas.drawCircle(mWidth / 2, mHeight / 2, minRadios, mBgCirclePaint);
            minRadios -= mSpaceVal;
        }
    }

    /**
     * 圆背景颜色
     *
     * @param canvas
     */
    private void drawBackgroundColor(Canvas canvas) {
        mBgCirclePaint.setColor(0xff00ddff);
        mBgCirclePaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(mWidth / 2, mHeight / 2, Math.min(mWidth / 2, mHeight / 2), mBgCirclePaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = h;
        mWidth = w;
        sweepGradient = new SweepGradient(mWidth / 2, mHeight / 2,
                new int[]{Color.TRANSPARENT, Color.WHITE}, new float[]{0.88f, 1f});
        mScanPaint.setShader(sweepGradient);
        mRadios = Math.min(mWidth / 2, mHeight / 2);
        mTotalCircle = Math.round(mRadios / mSpaceVal);
    }

    int dp2px(int val) {
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                val, getResources().getDisplayMetrics()));
    }

    public void start() {
        if (mAnimator != null && mAnimator.isRunning()) {
            return;
        }
        if (mAnimator == null) {
            this.mAnimator = ValueAnimator.ofInt(0, 360);
            this.mAnimator.setDuration(2500);
            this.mAnimator.setInterpolator(new LinearInterpolator());
            this.mAnimator.addUpdateListener(animation -> {
                Integer val = (Integer) animation.getAnimatedValue();
                System.out.println("val:" + val);
                mRateDegrees = val;
                int perVal = mTotalCircle <= 0 ? 0 : 360 / mTotalCircle;
                int current = 0;
                for (int i = 1; i <= mTotalCircle; i++) {
                    if (val <= perVal * i) {
                        current = i - 1;
                        break;
                    }
                }
                update(mTotalCircle - current, val);
            });
            this.mAnimator.setRepeatMode(ValueAnimator.RESTART);
            this.mAnimator.setRepeatCount(ValueAnimator.INFINITE);
        }
        mAnimator.start();
    }

    public void cancel() {
        if (mAnimator != null && mAnimator.isRunning()) {
            mAnimator.cancel();
        }
    }

    private void update(int selected, int rateDegrees) {
        this.mSelected = selected;
        this.mRateDegrees = rateDegrees;
        invalidate();
    }

}
